package com.example.studentapp.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.studentapp.R;

// android 手机里数据库
public class SQLiteDB extends SQLiteOpenHelper {
    Context context;
    public SQLiteDB(@Nullable Context context) {
        super(context, "stu_ass", null, 1);
        this.context = context;

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        if(sqLiteDatabase == null){
            sqLiteDatabase = context.openOrCreateDatabase("stu_ass",Context.MODE_PRIVATE,null);
        }
        try {
            // 创建注册用户
            String users_sql = "create table user_info(id integer primary key autoincrement," +
                    "phone varchar(11) not null unique, pwd varchar(12) not null, nickname varchar(20) not null," +
                    "email varchar(30) not null,head varchar(50), reg_date varchar(20) not null)";
            sqLiteDatabase.execSQL(users_sql);
            //创建课表列表
            String course_list_sql ="create table course_list(id integer primary key autoincrement,name varchar(40)  not null unique)";
            sqLiteDatabase.execSQL(course_list_sql);
            //创建课程详情表
            String course_detail_sql="create table course_detail(id integer primary key autoincrement," +
                    "course_list_id integer not null, name varchar(40) not null, place varchar(40) not null," +
                    "teacher varchar(30) not null,section integer not null)";
            sqLiteDatabase.execSQL(course_detail_sql);
            //创建日记表
            String diary_sql="create table diary(id integer primary key autoincrement," +
                    "title varchar(40) not null, weather varchar(20) not null," +
                    "content varchar(200) not null,upload_pic1  varchar(100),upload_pic2  varchar(100)," +
                    "upload_pic3  varchar(100), record_date varchar(30) not null)";
            sqLiteDatabase.execSQL(diary_sql);
            //创建财务表
            String money_sql="create table money(id integer primary key autoincrement," +
                    "cloth double not null, food double not null," +
                    "house double not null,use double  not null,walk double  not null," +
                    "other double not null,get_money double not null, record_date varchar(30) not null)";
            sqLiteDatabase.execSQL(money_sql);
        }catch (Exception e){
            Toast.makeText(context, context.getString(R.string.db_toast_add_error), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        onCreate(sqLiteDatabase);

    }
}
